package me.cxlr.qinlauncher2.dao;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;
import me.cxlr.qinlauncher2.model.Action;
import me.cxlr.qinlauncher2.model.DockAction;
import me.cxlr.qinlauncher2.model.KeyAction;

/**
 * KeyActionDao
 *
 * @author 酷安用户:山轮纸
 */
public class KeyActionDao {
    private Realm realm;

    /**
     * 保存
     *
     * @param keyActionList 物理按键
     */
    public void saveKeyAction(ArrayList<KeyAction> keyActionList) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        realm.beginTransaction();
        List<KeyAction> tmpList = realm.copyToRealm(keyActionList);
        realm.commitTransaction();
        realm.close();
    }

    /**
     * 修改 KeyAction
     *
     * @param keyAction 按键动作
     */
    public void updateKeyAction(KeyAction keyAction) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        realm.beginTransaction();
        KeyAction tmp = realm.copyToRealmOrUpdate(keyAction);
        realm.commitTransaction();
        realm.close();
    }

    /**
     * 修改 KeyAction
     *
     * @param keyAction 按键动作
     */
    public void updateKeyAction(List<KeyAction> keyAction) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        realm.beginTransaction();
        List<KeyAction> tmpList = realm.copyToRealmOrUpdate(keyAction);
        realm.commitTransaction();
        realm.close();
    }

    /**
     * 查询全部按键动作
     *
     * @return 全部按键动作
     */
    public List<KeyAction> findAllKeyAction() {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        List<KeyAction> results = realm.where(KeyAction.class).findAll();
        assert results != null;
        List<KeyAction> tmp = realm.copyFromRealm(results);
        realm.close();
        return tmp;
    }

    /**
     * 根据按键名称和按键类型查找按键动作
     *
     * @param keyName 按键名称
     * @param keyType 按键类型
     * @return 按键动作
     */
    public KeyAction findKeyActionByKeyNameAndKeyType(String keyName, int keyType) {
        try {
            realm = Realm.getInstance(RealmConfig.getRealmConfig());
            KeyAction results = realm.where(KeyAction.class)
                    .equalTo("keyName", keyName)
                    .and()
                    .equalTo("keyType", keyType)
                    .findFirst();
            assert results != null;
            KeyAction tmp = realm.copyFromRealm(results);
            realm.close();
            return tmp;
        } catch (Exception e) {
            Logger.e("findKeyActionByKeyNameAndKeyType:" + e.toString());
            return null;
        }
    }

    /**
     * 根据按键类型获取对应的功能
     *
     * @param keyType 按键类型 1:左右功能键 2:数字键短按 3:dpad 4:数字键长按
     * @return KeyAction
     */
    public List<KeyAction> findKeyActionListByKeyType(int keyType) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        List<KeyAction> tmpList = realm.where(KeyAction.class).equalTo("keyType", keyType).findAll();
        List<KeyAction> keyActionList = realm.copyFromRealm(tmpList);
        realm.close();
        return keyActionList;
    }

    /**
     * 根据包名查询DockAction
     *
     * @param packageName 包名
     * @return List<DockAction>
     */
    public List<KeyAction> findKeyActionByPackageName(String packageName) {
        List<KeyAction> keyActionList = new ArrayList<>(16);
        try {
            realm = Realm.getInstance(RealmConfig.getRealmConfig());
            RealmResults<KeyAction> results = realm.where(KeyAction.class)
                    .equalTo("app.packageName", packageName)
                    .findAll();
            if (results != null) {
                keyActionList = realm.copyFromRealm(results);
            }
            return keyActionList;
        } catch (Exception e) {
            Logger.e("findKeyActionByPackageName:" + e.toString());
            return keyActionList;
        } finally {
            realm.close();
        }

    }
}
